package com.drinks.yiniang.ui.fragment.user.invoice;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;

import com.drinks.yiniang.R;
import com.drinks.yiniang.io.api.InvoiceApiIO;
import com.drinks.yiniang.ui.widget.layout.TextCheckCheckLayout;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.fragment.BaseFragment;
import com.hansen.library.ui.widget.layout.TextEditLayout;
import com.hansen.library.utils.StringUtils;

/**
 * 提交发票
 * Created by han on 2019/11/16
 * A simple {@link Fragment} subclass.
 */
public class InvoiceSubmitFragment extends BaseFragment {

    //发票类型
    private TextCheckCheckLayout tcc_invoice_type;
    //发票抬头
    private TextEditLayout tel_invoice_title;
    //税号
    private TextEditLayout tel_invoice_number;
    //邮箱
    private TextEditLayout tel_invoice_email;
    //公司地址
    private TextEditLayout tel_invoice_address;
    //电话
    private TextEditLayout tel_invoice_mobile;
    //开户行
    private TextEditLayout tel_invoice_bank_name;
    //银行账号
    private TextEditLayout tel_invoice_bank_account;
    //发票内容
    private TextCheckCheckLayout tcc_invoice_content;

    private Button btn_invoice_submit_sure;

    //选择开票的订单id, 多个逗号分隔
    private String mOrderIds;

    public InvoiceSubmitFragment() {
        // Required empty public constructor
    }

    public static InvoiceSubmitFragment newInstance(String orderIds) {

        InvoiceSubmitFragment fragment = new InvoiceSubmitFragment();

        Bundle bundle = new Bundle();
        bundle.putString(Constants.KeyId, orderIds);

        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    protected int getResID() {
        return R.layout.fragment_invoice_submit;
    }

    @Override
    protected void initView(View mView) {
        tcc_invoice_type = mView.findViewById(R.id.tcc_invoice_type);
        tcc_invoice_content = mView.findViewById(R.id.tcc_invoice_content);

        tel_invoice_title = mView.findViewById(R.id.tel_invoice_title);
        tel_invoice_number = mView.findViewById(R.id.tel_invoice_number);
        tel_invoice_email = mView.findViewById(R.id.tel_invoice_email);
        tel_invoice_address = mView.findViewById(R.id.tel_invoice_address);
        tel_invoice_mobile = mView.findViewById(R.id.tel_invoice_mobile);
        tel_invoice_bank_name = mView.findViewById(R.id.tel_invoice_bank_name);
        tel_invoice_bank_account = mView.findViewById(R.id.tel_invoice_bank_account);

        btn_invoice_submit_sure = mView.findViewById(R.id.btn_invoice_submit_sure);
    }

    @Override
    protected void initData() {
        mContext = getActivity();

        if(getArguments() != null) {
            mOrderIds = getArguments().getString(Constants.KeyId);
        }

        tcc_invoice_type.setCheckStatus(TextCheckCheckLayout.STATUS_CHECK_RIGHT);
        tcc_invoice_content.setCheckStatus(TextCheckCheckLayout.STATUS_CHECK_RIGHT);
    }

    @Override
    protected void listener() {
        btn_invoice_submit_sure.setOnClickListener(this);
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.btn_invoice_submit_sure://确认
                submit();
                break;
            default:
                break;
        }
    }

    //提交申请
    private void submit() {
        String type = tcc_invoice_type.getCheckStatus() == TextCheckCheckLayout.STATUS_CHECK_LEFT ?
                "0" : tcc_invoice_type.getCheckStatus() == TextCheckCheckLayout.STATUS_CHECK_RIGHT ? "1" : null;

        if(type == null) {
            ToastUtils.showShort(getString(R.string.text_select_invoice_type));
            return;
        }

        String name = tel_invoice_title.getEditText();

        if(StringUtils.isEmpty(name)) {
            ToastUtils.showShort(getString(R.string.text_hint_input_invoice_title));
            return;
        }

        String number = tel_invoice_number.getEditText();

        if(StringUtils.isEmpty(number)) {
            ToastUtils.showShort(getString(R.string.text_hint_input_invoice_number));
            return;
        }

        String email = tel_invoice_email.getEditText();

        if(StringUtils.isEmpty(email)) {
            ToastUtils.showShort(getString(R.string.text_hint_input_receive_e_invoice_mail));
            return;
        }

        String address = tel_invoice_address.getEditText();

        if(StringUtils.isEmpty(email)) {
            ToastUtils.showShort(getString(R.string.text_hint_input_company_address));
            return;
        }

        String mobile = tel_invoice_mobile.getEditText();

        if(StringUtils.isEmpty(email)) {
            ToastUtils.showShort(getString(R.string.text_hint_input_company_tel));
            return;
        }

        String bankName = tel_invoice_bank_name.getEditText();

        if(StringUtils.isEmpty(email)) {
            ToastUtils.showShort(getString(R.string.text_hint_input_company_bank_name));
            return;
        }

        String bankAccount = tel_invoice_bank_account.getEditText();

        if(StringUtils.isEmpty(email)) {
            ToastUtils.showShort(getString(R.string.text_hint_input_bank_account));
            return;
        }

        String content = tcc_invoice_content.getCheckStatus() == TextCheckCheckLayout.STATUS_CHECK_LEFT ?
                "0" : tcc_invoice_content.getCheckStatus() == TextCheckCheckLayout.STATUS_CHECK_RIGHT ? "1" : null;

        if(content == null) {
            ToastUtils.showShort(getString(R.string.text_select_invoice_content));
            return;
        }

        if(StringUtils.isEmpty(mOrderIds)) {
            ToastUtils.showShort(getString(R.string.text_select_need_invoice_order));
            return;
        }

        showLoadingDialog();

        InvoiceApiIO.getInstance().submitInvoice(type, name, number, email, address, mobile, bankName, bankAccount,
                content, mOrderIds, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                ToastUtils.showShort(getString(R.string.text_submit_success));

                if(getActivity() != null) {
                    getActivity().finish();
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
